package com.eternal.base.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator
 * On 2019/9/2
 * Description:工具类
 *
 * @author ling0
 */
public class GuQiangUtil {

    /**
     * 格式化时间为csv开始结束时间
     *
     * @param millisecond 毫秒时间
     * @return time
     */
    public static String formatCsvTime(long millisecond) {
        Date d = new Date(millisecond);
        SimpleDateFormat sf = new SimpleDateFormat("h:mm aa @ MMM dd", Locale.ENGLISH);
        return sf.format(d).toUpperCase();
    }

    /**
     * 生成固强刻度盘显示时间
     *
     * @param timeMs
     * @return
     */
    public static String stringForTime(int timeMs) {
        int minutes = timeMs % 60;
        int hours = timeMs / 60;
        if (hours > 0) {
            return (hours > 9 ? hours : "0" + hours) + ":" + (minutes > 9 ? minutes : "0" + minutes);
        } else {
            return "00:" + (minutes > 9 ? minutes : "0" + minutes);
        }
    }

    /**
     * 生成固强刻度盘显示时间  小时小于10的时候不用补0
     *
     * @param timeMs
     * @return
     */
    public static String stringForTimeNoZero(int timeMs) {
        int minutes = timeMs % 60;
        int hours = timeMs / 60;
        if (hours > 0) {
            return hours + ":" + (minutes > 9 ? minutes : "0" + minutes);
        } else {
            return "0:" + (minutes > 9 ? minutes : "0" + minutes);
        }
    }

    /**
     * 生成固强刻度盘 日程模式
     *
     * @param timeMs
     * @return
     */
    public static String stringForTimeBySched(int timeMs) {
        int minutes = timeMs % 60;
        int hours = timeMs / 60;
        if (hours > 12) {
            return (hours - 12) + ":" + (minutes > 9 ? minutes : "0" + minutes) + " PM";
        } else {
            return (hours < 1 ? "00" : hours) + ":" + (minutes > 9 ? minutes : "0" + minutes) + " AM";
        }
    }

    public static String stringForTimeByAmPm(int time) {
        int minutes = time % 60;
        int hours = time / 60;
        if (hours == 0) {
            return String.format(Locale.ENGLISH, "12:%02d AM", minutes);
        } else if (hours >= 12) {
            if (hours == 24) {
                hours = 11;
                minutes = 59;
            } else if (hours > 12) {
                hours -= 12;
            }
            return String.format(Locale.ENGLISH, "%d:%02d PM", hours, minutes);
        } else {
            return String.format(Locale.ENGLISH, "%d:%02d AM", hours, minutes);
        }
    }

    public static String stringForTimeByCycle(int timeMs) {
        int minutes = timeMs % 60;
        int hours = timeMs / 60;
        if (hours == 0) {
            return minutes + "MIN";
        } else if (minutes == 0) {
            if (hours == 1) {
                return "1HR";
            } else {
                return hours + "HRS";
            }
        }
        if (hours < 1) {
            return hours + "HR : " + minutes + "MIN";
        } else {
            return hours + "HRS : " + minutes + "MIN";
        }
    }

    public static String stringForTimeByCycleSpace(int time) {
        int minutes = time % 60;
        int hours = time / 60;
        if (hours == 0) {
            return minutes + " MIN";
        } else if (minutes == 0) {
            if (hours == 1) {
                return "1 HR";
            } else {
                return hours + " HRS";
            }
        }
        if (hours < 1) {
            return hours + " HR : " + minutes + " MIN";
        } else {
            return hours + " HRS : " + minutes + " MIN";
        }
    }

    /**
     * 当前之间转换为AM PM时间
     *
     * @return
     */
    public static String getCurrentTimeFormatToStringByAmPm() {
        SimpleDateFormat aa = new SimpleDateFormat("K:mm aa", Locale.ENGLISH);
        return aa.format(new Date());
    }

    public static String getCurrentTimeFormatNoAmPm() {
        SimpleDateFormat aa = new SimpleDateFormat("K:mm", Locale.ENGLISH);
        return aa.format(new Date());
    }

    /**
     * 格式化时间为小时和分钟秒
     *
     * @param timeSeconds 单位  秒
     * @return HRS, MINS,
     */
    public static String formatTimeToHourAndMin(long timeSeconds) {
        int hour = (int) timeSeconds / 3600;
        int mints = (int) (timeSeconds % 3600) / 60;
        String hourStr = hour > 0 ? (hour > 1 ? hour + "HRS" : hour + "HR") : "";
        String mintStr = mints > 0 ? (mints > 1 ? mints + "MINS" : mints + "MIN") : "";
        return hourStr + " : " + mintStr;
    }

    /**
     * 格式化时间为小时和分钟秒
     *
     * @param timeSeconds 单位  分
     * @return HRS, MINS,
     */
    public static String formatTimeToHourAndMinByAdvance(String timeSeconds) {
        int time = Integer.parseInt(timeSeconds);
        int hour = time / 60;
        int mints = time % 60;
//        String hourStr = hour > 0 ? (hour > 1 ? hour + " HRS " : hour + " HR ") : "0";
        String hourStr = hour > 1 ? hour + " HRS " : hour + " HR ";
        String mintStr = mints + " MIN ";
//        String mintStr = mints > 0 ? (mints > 1 ? mints + " MINS " : mints + " MIN ") : "";
        return hourStr + " : " + mintStr;
    }

    public static String formatTimeToHourMin(char second) {
        int hour = second / 60;
        int mints = second % 60;
        String mintStr = mints + " MIN ";
        if (hour == 0) {
            return mintStr;
        }
        String hourStr = hour > 1 ? hour + " HRS " : hour + " HR ";
        return hourStr + " : " + mintStr;
    }


    /**
     * 时间转成long
     * yyyy/MM/dd HH:mm
     *
     * @param dateString
     * @return
     */
    public static long getString2Date(String dateString) {
        String pattern = "yyyy/MM/dd HH:mm";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * long类型转 str
     *
     * @param time
     * @return
     */
    public static String parseLong2Date(long time) {
        String pattern = "yyyy/MM/dd HH:mm";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        Date date = new Date(time);
        try {
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
